<?php

namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Cafe extends Model
{
    use SoftDeletes;
    // 获取多大范围内的咖啡馆
    const DISTANCE = 10;
    // 咖啡馆上线
    const ONLINE = 1;
    // 咖啡馆下线
    const OFFLINE = 0;
    // 审核铜鼓
    const PASS = 1;
    // 未审核通过
    const NOT_PASS = 0;

    // 咖啡馆status文本
    static protected $statusArray = [
      self::ONLINE => '在线',
      self::OFFLINE  => '下线'
    ];

	//指定表名
	protected $table='cafe';
    // 开启自动时间戳
    public $timestamps = true;
    // 日期都存储为时间戳
    protected $dateFormat = 'U';

	public function message()
    {
        return $this->hasMany('App\Message', 'user_id', 'id');
    }

	public function supplys()
	{
		return $this->belongsToMany('App\Supplys', 'cafe_supply', 'cafe_id', 'supply_id');
	}

	public function photo(){
		return $this->hasMany('App\Photo','cafe_id','id');
	}

	public function user()
	{
		return $this->belongsToMany('App\Users','cafe_mapping_user','cafe_id','user_id')->withPivot(['shang']);
	}

	public function followedUser()
	{
		return $this->belongsToMany('App\Users','user_follow_cafe','cafe_id','user_id');
	}

	public function visitedUsers()
	{
		return $this->belongsToMany('App\Users','user_visited_cafe','cafe_id','user_id');
	}

	public function infoPartners()
	{
		return $this->belongsToMany('App\Partner','cafe_partner','cafe_id','partner_id');
	}

	public function city()
    {
        return $this->hasOne('App\City', 'code', 'citycode');
    }

    public function district()
    {
        return $this->hasOne('App\District', 'adcode', 'adcode');
    }

	// 追加status_text字段,status的文本类型
	public function getStatusTextAttribute()
    {
        return self::$statusArray[$this->attributes['status']];
    }

    // 追加status_bool字段,status的布尔类型
    public function getStatusBoolAttribute()
    {
        return $this->attributes['status'] ? 'true' : false;
    }

    // 追加show_logo_bool字段,status的布尔类型
    public function getShowLogoBoolAttribute()
    {
        return $this->attributes['show_logo'] ? 'true' : false;
    }

    public function mark()
    {
        return $this->belongsToMany('App\Mark', 'cafe_mapping_mark', 'cafe_id', 'mark_id');
    }

    public function video()
    {
        return $this->hasMany('App\Video', 'cafe_id', 'id');
    }
}
